'''
Author: 神仙盼盼 3027925806@qq.com
Date: 2024-03-07 13:00:47
LastEditors: 神仙盼盼 3027925806@qq.com
LastEditTime: 2024-03-07 13:00:52
FilePath: \Leetcode\day8\124.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxPathSum(self, root: Optional[TreeNode]) -> int:
        self.answer = -1000
        self.maxroot(root)
        return self.answer

    def maxroot(self, root):
        if root == None:
            return 0
        else:
            left = self.maxroot(root.left)
            right = self.maxroot(root.right)
            self.answer = max(self.answer, root.val + left + right)
            return max(max(left, right) + root.val, 0)